const { app, BrowserWindow, Menu, ipcMain } = require('electron')
const path = require('path')
const remote = require("@electron/remote/main");
remote.initialize();
const NODE_ENV = process.env.NODE_ENV




var win;

/**
 * @Description: electron程序入口
 * @Author: Etc.End
 * @Copyright: TigerSong
 * @CreationDate 2023-05-20 14:39:26
 */
Menu.setApplicationMenu(null) // null值取消顶部菜单栏

async function createWindow() {
    win = new BrowserWindow({
        icon: './public/sw.png',
        center: true,
        skipTaskbar: false,
        transparent: false,
        frame: false,
        fullscreen: true, // 全屏显示
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: true,
            webSecurity: false,
            preload: path.join(__dirname, '../preload.js')
        }
    });

    win.loadURL(
        NODE_ENV === 'development' ? 'http://localhost:5173/' : `file://${path.join(__dirname, '../dist/index.html')}`
    )

    // if (NODE_ENV === 'development') {
    //     win.webContents.openDevTools()
    // }
    win.webContents.openDevTools()
    remote.enable(win.webContents);

    // 增加ipc通信 主线程信息传递到渲染线程中
    ipcMain.on('getDataRequest', (event,args) => {
        console.log('---------------', event)
        // 处理请求，比如从数据库或文件中获取数据
        const responseData = "This is the response data";
        // 将数据发送回渲染进程
        event.reply('putDataResponse', args);
    });
}

app.whenReady().then(createWindow);

/**
 * @Description: 限制只能打开一个页面
 * @CreationDate 2023-05-20 14:35:52
 */
var gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
    app.quit();
} else {
    app.on('second-instance', function(event, commandLine, workingDirectory) {
        if (win) {
            if (win.isMinimized()) win.restore();
            win.focus();
        }
    });
}

app.on('window-all-closed', function() {
    if (process.platform !== 'darwin') app.quit();
});
